﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Gift.WebApp;
using Gift.DAL;
using Gift.Model;

namespace WebApp.admin.order
{
    public partial class meterialOrderAdd02 :BaseAdmin
    {

        DALCard theCard;
        List<tbProfessionAreaType> lstProfessionType = new List<tbProfessionAreaType>();

        tbGoods oGoods = new tbGoods();
         tbCompany oCompany = new tbCompany();

        public string address = "";
        public string carid = "0";
        public string carinfo = "";
        public string cname = "";
        public string email = "";
        public string istrue = "0";
        public string phone = "";
        public string post = "";
        public string score1 = "0";
        public string score2 = "0";
        public string score3 = "0";
        public string shipmsg = "";
        public string tel = "";
        public string totalprice = "0";
        public string totalscore = "0";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (theCard == null)
            {
                theCard = new DALCard(base.Administrator.CompanyCode);
            }
            if (!IsPostBack)
            {      
                this.txtSendDate.Text = DateTime.Now.AddDays(2).ToString("yyyy-MM-dd");        
                this.GenerateOrderCode();
                this.AddProdutToCar();
                this.BindProductList();
                this.BindCompany();
            }     
        }

        void BindCompany()
        {

            oCompany = BaseFactory.theCompany.GetModel(int.Parse(base.Administrator.CompanyId));
            this.txtCompanyName.Text = oCompany.Name;
            this.txtCompanyName.ReadOnly = true;
            this.txtUserName.Text = oCompany.Linker;
            this.txtPhone.Text = oCompany.Telephone;
            this.txtAddress.Text = oCompany.Address;

        }


        void GenerateOrderCode()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["orderCode"]))
            {
                this.lblOrderCode.Text = Request.QueryString["orderCode"];
            }
            else
            {
                this.lblOrderCode.Text = BaseFactory.theOrderAccount.GenerateOrderCodeForMeterial().ToString();
            }
        }



      
        void BindProductList()
        {
            if (!string.IsNullOrEmpty(this.lblOrderCode.Text.Trim()))
            {
                decimal dOnSale = base.TempOrderOnSale;
                DataTable dtResult = BaseFactory.theCar.GetListByOrderCode(long.Parse(this.lblOrderCode.Text.Trim()), dOnSale);
                this.GridView1.DataSource = dtResult.DefaultView;
                this.GridView1.DataBind();
            }
        
        }


        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string sContents = "";// 暂未添加
            if (e.CommandName == "edi")
            {
                if (ViewState["editRowIndex"] != null)
                {
                    int iRowIndex = int.Parse(ViewState["editRowIndex"].ToString());
                    LinkButton lbtEdit = GridView1.Rows[iRowIndex].FindControl("lbtEdit") as LinkButton;
                    LinkButton lbtSave = GridView1.Rows[iRowIndex].FindControl("lbtSave") as LinkButton;
                    LinkButton lbtCancel = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("lbtCancel") as LinkButton;
                    TextBox txtNum = GridView1.Rows[iRowIndex].FindControl("txtNum") as TextBox;
                    lbtEdit.Visible = true;
                    lbtSave.Visible = false;
                    lbtCancel.Visible = false;
                    txtNum.Text = ViewState["num"].ToString();
                    txtNum.Enabled = false; ;
                }
                LinkButton lbtEdit2 = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("lbtEdit") as LinkButton;
                LinkButton lbtSave2 = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("lbtSave") as LinkButton;
                LinkButton lbtCancel2 = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("lbtCancel") as LinkButton;
                TextBox txtNum2 = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("txtNum") as TextBox;
                lbtEdit2.Visible = false;
                lbtSave2.Visible = true;
                lbtCancel2.Visible = true;
                txtNum2.Enabled = true;
                ViewState["editRowIndex"] = e.CommandArgument.ToString();
                ViewState["num"] = txtNum2.Text;
            }
            else if (e.CommandName == "sav")
            {
                LinkButton lbtEdit3 = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("lbtEdit") as LinkButton;
                LinkButton lbtSave3 = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("lbtSave") as LinkButton;
                LinkButton lbtCance3 = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("lbtCancel") as LinkButton;
                TextBox txtNum3 = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("txtNum") as TextBox;
                 Label lblPrice3 = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("lblPrice") as Label;
                int iProductId =  int.Parse(((HyperLink)GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[0].Controls[0]).Text);
                if (txtNum3.Text.Trim() == "0")
                {
                    BaseFactory.theCar.Delete(long.Parse(this.lblOrderCode.Text.Trim()), iProductId);
                }
                else
                {
                    tbCar oCar = BaseFactory.theCar.GetModel(long.Parse(this.lblOrderCode.Text.Trim()), iProductId);
                    oCar.orderId = long.Parse( this.lblOrderCode.Text.Trim());
                    oCar.ProdNumber = int.Parse(txtNum3.Text);
                    oCar.ProdPrice = decimal.Parse(lblPrice3.Text);
                    oCar.ProductId = iProductId;
                    BaseFactory.theCar.Update(oCar);
                }

                lbtEdit3.Visible = true;
                lbtSave3.Visible = false;
                lbtCance3.Visible = false;
                txtNum3.Enabled = false;
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"保存成功！\");</script>");
                this.BindProductList();
               
            }
            else if (e.CommandName == "can")
            {
                LinkButton lbtEdit4 = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("lbtEdit") as LinkButton;
                LinkButton lbtSave4 = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("lbtSave") as LinkButton;
                LinkButton lbtCancel4 = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("lbtCancel") as LinkButton;
                TextBox txtNum4 = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("txtNum") as TextBox;
                lbtEdit4.Visible = true;
                lbtSave4.Visible = false;
                lbtCancel4.Visible = false;
                txtNum4.Enabled = false;
            }
            else if (e.CommandName == "del")
            {
                int iProductId = int.Parse(((HyperLink)GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[0].Controls[0]).Text);
                BaseFactory.theCar.Delete(long.Parse(this.lblOrderCode.Text.Trim()), iProductId);
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"删除成功！\");</script>");
                this.BindProductList();
            }
        }

        protected void ibtnSubmit_Click(object sender, ImageClickEventArgs e)
        {
            if (this.lblOrderCode.Text == "")
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"请选择添加新订单或者选择未完成的订单！\");</script>");
                return;
            }
            tbCardOrder oOrder = new tbCardOrder();
            tbCar oCar = new tbCar();
            decimal dTotalPrice = BaseFactory.theCar.GetSpendTotalPrice(long.Parse(this.lblOrderCode.Text.Trim()));
            oOrder.orderid = long.Parse(this.lblOrderCode.Text.Trim());
            oOrder.pay = 16;//付款方式为实物
            oOrder.states = 0;
            oOrder.cname = base.Administrator.AdminUser;
            oOrder.cname1 = this.txtUserName.Text.Trim();
            oOrder.tel1 = this.txtTel.Text.Trim();
            oOrder.phone1 = this.txtPhone.Text.Trim();
            oOrder.post1 = this.txtPost.Text.Trim();
            oOrder.address1 = this.AddressArea1.GetProvenceArea() + this.txtAddress.Text.Trim();
            oOrder.Senddate = this.txtSendDate.Text.Trim() == "" ? DateTime.Now.AddDays(2) : DateTime.Parse(this.txtSendDate.Text.Trim()); 
            oOrder.ordertime = DateTime.Now;
            oOrder.languages = 1;
            oOrder.CompanyId = int.Parse(base.Administrator.CompanyId);
            oOrder.totalprice =dTotalPrice.ToString();
            oOrder.content = this.txtRemark.Text.ToString();
            BaseFactory.theCardOrder.Add(oOrder);

            BaseFactory.theCar.AddTempToRealForMeterial(oOrder.orderid);
            base.TempOrderOnSale = 1;
            base.Response.Write("<script  language='javascript'>alert('提交订单成功!请记住订单编号(" + oOrder.orderid.ToString() + ")以便查询!');window.location.href='meterialOrderList.aspx'</script>");

        }


         void AddProdutToCar()
        {
            try
            {
                //添加数据
                tbCar oCar = new tbCar();

                DataTable dtGoodsEntitys;
                decimal dOnSale = BaseFactory.theCompany.GetCompanyOnSale(base.Administrator.CompanyId);

                string[] sIds = base.Administrator.MeteriaOrderCardIds.Split(',');                     
                foreach (string proId in sIds)
                {
                    dtGoodsEntitys = BaseFactory.theGoodsEntitys.GetGoodsEntitysByGoods(int.Parse(proId));
                    oGoods = BaseFactory.theGoods.GetModel(int.Parse(proId));
                    oCar = new tbCar();
                    oCar.orderId = long.Parse(this.lblOrderCode.Text.Trim());
                    oCar.ProdNumber = 1;
                    oCar.ProdPrice = decimal.Parse(dtGoodsEntitys.Rows[0]["salePrice"].ToString()) * dOnSale;
                    oCar.ProductId = int.Parse(proId);
                    oCar.UpdateDate = DateTime.Now;
                    BaseFactory.theCar.Add(oCar);
                }
                this.BindProductList();
            }
            catch
            {
                Response.Redirect("meterialOrderAdd.aspx");
            }
              
        }
  



    }
}